<?php
/**
 * Created by PhpStorm.
 * User: henly
 * Date: 14-4-18
 * Time: 下午5:38
 * E-mail: huwenlong_92@hotmail.com
 */
include 'curl.class.php';
include 'zip.class.php';
$curl = curl::getInstance();
$zip = new Zip();

//$app_id = 500088146;
$app_id = 364709193;

$url = 'http://itunes.apple.com/cn/lookup?id='.$app_id;
$data = json_decode($curl->get($url),true);
$data = $data['results'][0];

header("Content-type: text/html; charset=utf-8");
if($data){
	
	//支持型号
	$supportedDevices = isset($data['supportedDevices']) ? $data['supportedDevices'] : "";
	
	//软件名称
	$trackName  = isset($data['trackName']) ? $data['trackName'] : "";

	//iphone 展示图片
	$screenshotUrls = isset($data['screenshotUrls']) ? $data['screenshotUrls'] : "";
	// ipad 展示图片
	$ipadScreenshotUrls = isset($data['ipadScreenshotUrls']) ? $data['ipadScreenshotUrls'] : "";
	//图标
	$artworkUrl60 = isset($data['artworkUrl60']) ? $data['artworkUrl60'] : "";
	//软件版本
	$version = isset($data['version']) ? $data['version'] : "";
	// 软件描述
	$description = isset($data['description']) ? $data['description'] : "";
	// 软件大小
	$fileSizeBytes = isset($data['fileSizeBytes']) ? $data['fileSizeBytes'] : "";
	// 最近发布功能 描述
	$releaseNotes = isset($data['releaseNotes']) ? $data['releaseNotes'] : "";
	// 发布日期
	$releaseDate = isset($data['releaseDate']) ? $data['releaseDate'] : "";
	
	$path = "E:/henly/"	;
	$file = $trackName.'_'.date('Y-m-d');
	//如果存在则删除目录
	if(is_dir($path.$file)){
		del_dir($path.$file);
	}
	//重新创建 不成功提示错误	
	if(!mkdir($path.$file,0775)){
		echo 'failed to create';
		die;
	}
	//创建图片文件夹
	if(!mkdir($path.$file.'/images',0775)){
		echo 'failed to create';
		die;
	}
	
	// 生成文件 写入txt文件中
	$str  = '软件名称：' . $trackName . PHP_EOL.PHP_EOL;
	$str .= '支持型号:' .implode(' |  ',$supportedDevices).PHP_EOL.PHP_EOL;
	$str .= '软件版本:' .$version .PHP_EOL.PHP_EOL;
	$str .= '软件发布日期:' .$releaseDate.PHP_EOL.PHP_EOL;
	$str .= '软件描述:' .PHP_EOL.$description.PHP_EOL.PHP_EOL;
	$str .= '软件大小:' .$fileSizeBytes .'MB' .PHP_EOL.PHP_EOL;
	$str .= '最近发布功能描述:'.PHP_EOL .$releaseNotes. PHP_EOL.PHP_EOL;

	$str .= '图标网址:'. $artworkUrl60 . PHP_EOL.PHP_EOL;

	$image = $curl->get($artworkUrl60);
	file_put_contents($path.$file.'/images/icon.png',$image);

	$str .= 'iphone 展示图片网址:' .PHP_EOL;
	foreach($screenshotUrls as $key=> $val){
		$str .= $val .PHP_EOL;
		$image = $curl->get($val);
		file_put_contents($path.$file.'/images/iphone_'.$key.'.png',$image);
	}
	$str .= 'ipad 展示图片网址:'.PHP_EOL;
	foreach($ipadScreenshotUrls as $val){
		$str .= $val .PHP_EOL;	
		$image = $curl->get($val);
		file_put_contents($path.$file.'/images/ipad_'.$key.'.png',$image);
	}
	$file_name = $path.$file.'/readme.txt';
	file_put_contents($file_name,$str,FILE_APPEND | LOCK_EX);


	$zip->create_zip($path.$file,'aaa');

	
	
	
	

}

/**
 * 删除文件(包含文件里面的内容)
 *  unlink()函数用于删除文件  return true | false
 *  rmdir()函数用于删除空的目录 (该目录必须为空，并且要有相应的权限)
 * @param $dir   要删除的目录路径
 */
function del_dir($dir){
    //打开目录
    $path =  opendir($dir);
    //遍历读文件(过滤.|..)
    while($file = readdir($path)){
        if($file != "." && $file != ".."){
            $fullpath = $dir . "/" .$file;
            //检查是否为目录
            if(!is_dir($fullpath)){
                //删除文件
                @unlink($fullpath);
            }else{
                del_dir($fullpath);
            }
        }
    }
    //关闭文件资源
    closedir($path);
    //删除目录
    if(rmdir($dir)){
        return true;
    }else{
        return false;
    }
}


?>